package br.com.sovi.gwt.client.rastreamento;

import java.util.ArrayList;
import java.util.List;

import br.com.sovi.gwt.client.entity.Carro;
import br.com.sovi.gwt.client.entity.Point;

import com.gwtext.client.data.Node;
import com.gwtext.client.data.NodeTraversalCallback;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;

public class RastreamentoCarroTreePanel extends TreePanel {

	private List<Carro> carros = new ArrayList<Carro>();

	private CarroCheckChangeListener carroCheckChangeListener;

	TreeNode root = new TreeNode();

	{
		carros.add(new Carro(new Point(-23.42, -51.88), "Chevrolet", "Corsa",
				"3458754642468", "APN-2345"));
		carros.add(new Carro(new Point(-23.43, -51.88), "Volkswagen", "Gol",
				"3458754642468", "PRQ-5643"));
		carros.add(new Carro(new Point(-23.42, -51.89), "Fiat", "Palio",
				"3458754642468", "TER-3232"));
		carros.add(new Carro(new Point(-23.43, -51.89), "Ford", "Fiesta",
				"3458754642468", "JKL-1743"));
	}

	public RastreamentoCarroTreePanel() {
		// this.setTitle("Rastreamento");
		// this.setCollapsible(true);
		// this.setIconCls("world-icon");
		this.setHeight("100%");
		this.setWidth(200);
		this.setAnimate(true);
		this.setEnableDD(true);
		this.setContainerScroll(true);
		this.setAutoScroll(true);
		this.setRootVisible(false);
		this.setFrame(true);

		this.addListener(new TreePanelListenerAdapter() {
			@Override
			public void onCheckChange(TreeNode node, final boolean checked) {
				if (node.getChildNodes().length > 0) {
					node.eachChild(new NodeTraversalCallback() {
						@Override
						public boolean execute(Node child) {
							if (child instanceof TreeNode) {
								onCheckChange((TreeNode) child, checked);
								((TreeNode) child).getUI().toggleCheck(checked);
							}
							return true;
						}
					});
				} else {
					if (getCarroCheckChangeListener() != null) {
						String text = node.getText();
						getCarroCheckChangeListener().onCheckChange(
								getCarroByNodeText(text), checked);
					}
				}
			}
		});

		populateTree();
	}

	protected Carro getCarroByNodeText(String text) {
		for (Carro carro : carros) {
			if (text.equals(carro.getModelo())) {
				return carro;
			}
		}

		return null;
	}

	private void populateTree() {

		TreeNode carrosPasseioNode = new TreeNode("Carros de Passeio");
		carrosPasseioNode.setChecked(false);
		carrosPasseioNode.setExpanded(true);

		for (Carro carro : carros) {
			TreeNode treeNode = new TreeNode(carro.getModelo());
			treeNode.setChecked(false);
			carrosPasseioNode.appendChild(treeNode);
		}

		TreeNode caminhoesNode = new TreeNode("Caminhões");
		caminhoesNode.setExpanded(true);

		Carro caminhao = new Carro(new Point(-23.47, -51.89), "Mercedes-Benz",
				"K123", "3458754642468", "TKL-5454");

		TreeNode caminhaoNode = new TreeNode(caminhao.getModelo());
		caminhaoNode.setChecked(false);
		caminhoesNode.appendChild(caminhaoNode);
		caminhoesNode.setChecked(false);

		root.appendChild(carrosPasseioNode);
		root.appendChild(caminhoesNode);

		this.setRootNode(root);
	}

	public void setCarroCheckChangeListener(
			CarroCheckChangeListener carroCheckChangeListener) {
		this.carroCheckChangeListener = carroCheckChangeListener;
	}

	public CarroCheckChangeListener getCarroCheckChangeListener() {
		return carroCheckChangeListener;
	}

}
